package com.module.app.pop.anim;

import android.animation.ArgbEvaluator;
import android.animation.ValueAnimator;

import androidx.interpolator.view.animation.FastOutSlowInInterpolator;


import com.lxj.xpopup.animator.PopupAnimator;

/**
 * Create by dance, at 2018/12/9
 * Description: 设置视图透明化
 */
public class PopAlphaBgAnimator extends PopupAnimator {

    public ArgbEvaluator argbEvaluator = new ArgbEvaluator();
    public int startAlpha = 0;
    public int endAlpha = 1;
    private int duration = 500;
    private boolean isShowAnim = true;//显示动画
    private boolean isHideAnim = true;//隐藏动画

    public PopAlphaBgAnimator() {
        super();
    }

    public PopAlphaBgAnimator(int startAlpha, int endAlpha) {
        super();
        this.startAlpha = startAlpha;
        this.endAlpha = endAlpha;
    }

    public PopAlphaBgAnimator(boolean isShowAnim, boolean isHideAnim) {
        super();
        this.isShowAnim = isShowAnim;
        this.isHideAnim = isHideAnim;
    }

    @Override
    public void initAnimator() {
        if (isShowAnim){
            targetView.setAlpha(startAlpha);
        }
    }

    @Override
    public void animateShow() {
        ValueAnimator animator = ValueAnimator.ofFloat(isShowAnim ? startAlpha : 1, isShowAnim ? endAlpha : 1);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                if (targetView != null) {
                    targetView.setAlpha((Float) animation.getAnimatedValue());
                }
            }
        });
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.setDuration(duration).start();
    }

    @Override
    public void animateDismiss() {
        ValueAnimator animator = ValueAnimator.ofFloat(isHideAnim ? endAlpha : 0, isHideAnim ? startAlpha : 0);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                try {
                    //销毁的时候可能会调用，注意抛出
                    targetView.setAlpha((Float) animation.getAnimatedValue());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        animator.setInterpolator(new FastOutSlowInInterpolator());
        animator.setDuration(duration).start();
    }

    @Override
    public int getDuration() {
        return duration;
    }

    public void setDuration(int duration) {
        this.duration = duration;
    }

    public boolean isShowAnim() {
        return isShowAnim;
    }

    public void setShowAnim(boolean showAnim) {
        isShowAnim = showAnim;
    }

    public boolean isHideAnim() {
        return isHideAnim;
    }

    public void setHideAnim(boolean hideAnim) {
        isHideAnim = hideAnim;
    }
}
